﻿//****************************************************************************
// Copyright (c) Microsoft Corporation. All rights reserved.
//****************************************************************************
using System;
using System.Reflection;
using System.Collections.Generic;
using System.Text;
using Microsoft.FxCop.Sdk;

namespace Microsoft.FxCop.Rules.ASP.Security
{
    class EnableViewStateMacShouldBeTrue : BasePageIntrospectionRule
    {
        #region *** Constructor ***
        public EnableViewStateMacShouldBeTrue()
            : base("EnableViewStateMacShouldBeTrue")
        {
        }
        
        #endregion
        #region *** Methods ***

        #region *** BasePageRule ***
        protected override void CheckBuildControlTree(Method buildTreeMethod)
        {
            Instruction currentInstruction;
            Instruction peekInstruction;

            for (int i = 0; i < buildTreeMethod.Instructions.Count; i++)
            {
                currentInstruction = buildTreeMethod.Instructions[i];

                if (currentInstruction.OpCode == OpCode.Callvirt)
                {
                    if ((currentInstruction.Value as Method).Name.Name == "set_EnableViewStateMac")
                    {
                        peekInstruction = buildTreeMethod.Instructions[i - 1];

                        if (peekInstruction.OpCode == OpCode.Ldc_I4_0)
                        {
                            ReportProblem();
                        }

                        // we found the set_EnableViewStateMac. This will only occur once so we can return
                        return;
                    }
                }
            }
        }
        #endregion

        #endregion
    }
}
